package com.ruoyi.mes.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 打标机 （功能比较局限）
 */
public class MarkingMachineUtils {
    public static void MarkingMachineMarkingWord(String proceName){
        if (!proceName.equals("打标机打标")) {
            return;
        }

        // API地址
        String url = "http://192.168.1.101:9999/printimage";

        // 请求数据
        String requestData = "{\"output\":\"233.ezd\"}";

        try {
            // 创建URL对象
            URL apiUrl = new URL(url);

            // 打开URL连接
            HttpURLConnection connection = (HttpURLConnection) apiUrl.openConnection();

            // 设置请求方法为POST
            connection.setRequestMethod("POST");

            // 设置请求头部信息
            connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");

            // 允许输出流写入数据
            connection.setDoOutput(true);

            // 将请求数据写入输出流
            connection.getOutputStream().write(requestData.getBytes("UTF-8"));

            // 发送HTTP请求
            connection.connect();

            // 读取响应数据
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                System.out.println(response.toString());
            }

            reader.close();

            // 输出响应结果
            System.out.println("Response body: " + response.toString());

            // 断开连接
            connection.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
